$(function(){
	showCondition(true, 1, 0, 1, [1, 1, 0, 0, 0,0]);//加载查询条件
	scrollTo(0,0);
	tabId=$("#tabContent > .tab-pane.active").attr("id");
	ajaxCntDepFeeStage = {"dep":0,"clinic":0};
	totalQuota = [
	                  {name:"产能指标", 
	                   data:[
	                         {"quotaCode":"TotalCaseCnt", "quotaName":"患者数"}, 
	                         {"quotaCode":"TotalFee", "quotaName":"总费用(万元)"},
	                         {"quotaCode":"AvgFee", "quotaName":"次均费用(元)"},
	                         {"quotaCode":"DrugFee", "quotaName":"药品费用(万元)"}, 
	                         {"quotaCode":"DrugRatio", "quotaName":"药占比(%)"},
	                         {"quotaCode":"MaterialFee", "quotaName":"耗材费(万元)"}, 
	                         {"quotaCode":"MaterialRatio", "quotaName":"耗占比(%)"},
	                         {"quotaCode":"OrderQuantity", "quotaName":"医嘱项数"}
	                         ]
	                  }
	                  ];
	
	$('#'+tabId).ajaxStop(function(){
		
		reloadNav();
	});
	orderSpecialDep='';
	$.post(contextPath+'/feeStage/getOrderSpecialDep.dhc',function(result){
		if(result.data.length>0){
			for(var o in result.data){
				orderSpecialDep+="'"+result.data[o].DEPDESC+"',";
			}
		}
		paramTabPage[tabId]['orderSpecialDep'] = orderSpecialDep==''?'':orderSpecialDep.substring(0,orderSpecialDep.length-1);
	});
	//科室组列表
	$.getJSON(contextPath+'/DepGroup/findAllDepGroup.dhc',
		function(dep){
		$.each(dep.data,function(i,n){
			$('#'+tabId+' #depGroupList').append('<li><a class="depGroup" title="'+n.depGroupDesc+'" id="'+n.depGroupCode
					+'"><i class="icon-user-md icon"></i>&nbsp;&nbsp;'+n.depGroupDesc+'</a></li>');
			
		});
		var parDepGroupCode;
		if(commonLayerConfig[0]=="0" && commonLayerConfig[1]=="1"){
			$('#'+tabId+' #depGroupList #'+paramTabPage[tabId].pardepgroupCode).addClass('depActive');
			$('#' + tabId + ' #depGroupListDiv').css("display","none");
			parDepGroupCode="";
			//科室列表
			$.getJSON(contextPath+'/Department/findAllDep.dhc',
				{'parDepGroupCode':parDepGroupCode},
				function(dep){
					$('#'+tabId+' #depList').html('');
					$.each(dep.data,function(i,n){
						$('#'+tabId+' #depList').append('<li><a class="dep" title="'+n.depName+'" id="'+n.depCode+'">'+n.depName+'</a></li>');
					});
					if(paramTabPage[tabId].pardepCode != '' && paramTabPage[tabId].pardepCode != undefined){
						$('#'+tabId+' #depList #'+paramTabPage[tabId].pardepCode).click();
					} else {
						$('#'+tabId+' #depList').children().first().find('a').click();
					}
			});
		}else if(commonLayerConfig[0]=="1"){
			$('#' + tabId + ' #depGroupListDiv').css("display","block");
			parDepGroupCode=paramTabPage[tabId].pardepgroupCode;
			if(paramTabPage[tabId].pardepgroupCode == '' || paramTabPage[tabId].pardepgroupCode == undefined){
				$('#'+tabId+' #depGroupList').children().first().find('a').click();
			} else {
				$('#'+tabId+' #depGroupList #'+paramTabPage[tabId].pardepgroupCode).addClass('depActive');
				$('#' + tabId + ' #depGroupListDiv').css("display","block");
				parDepGroupCode=paramTabPage[tabId].pardepgroupCode;
				//科室列表
				$.getJSON(contextPath+'/Department/findAllDep.dhc',
					{'parDepGroupCode':parDepGroupCode},
					function(dep){
						$('#'+tabId+' #depList').html('');
						$.each(dep.data,function(i,n){
							$('#'+tabId+' #depList').append('<li><a class="dep" title="'+n.depName+'" id="'+n.depCode+'">'+n.depName+'</a></li>');
						});
						if(paramTabPage[tabId].pardepCode != '' && paramTabPage[tabId].pardepCode != undefined){
							$('#'+tabId+' #depList #'+paramTabPage[tabId].pardepCode).click();
						} else {
							$('#'+tabId+' #depList').children().first().find('a').click();
						}
				});
			}
		}
	});
	 
	//科室组点击事件
	$('#'+tabId).off('click','.depGroup');
	$('#'+tabId).on('click','.depGroup',function(){
		$('#'+tabId+' .dep').removeClass('depActive');
		$('#'+tabId+' .depGroup').removeClass('depActive');
		$(this).addClass('depActive');
		
		var pardepgroupCode = $(this).attr('id');
		var depGroupName = $(this).attr('title');
		paramTabPage[tabId]['pardepgroupCode'] = pardepgroupCode;
		paramTabPage[tabId]['pardepCode'] = '';
		paramTabPage[tabId]['depName'] = '';
		paramTabPage[tabId]['groupName'] = '';
		$('#'+tabId+' .currDepOrDepgroup').html('<i class="fa fa-stethoscope"></i>'+depGroupName);
		
		//科室列表
		$.getJSON(contextPath+'/Department/findAllDep.dhc',{'parDepGroupCode':pardepgroupCode},function(dep){
				$('#'+tabId+' #depList').html('');
				$.each(dep.data,function(i,n){
					$('#'+tabId+' #depList').append('<li><a class="dep" title="'+n.depName+'" id="'+n.depCode+'">'+n.depName+'</a></li>');
				});
				if(dep.data[0]==null){
					loadQuotaData('depGroup',pardepgroupCode,depGroupName);
				}
				else{
					$('#'+tabId+' #depList').children().first().find('a').click();
				}
		});
		
	});
	//科室点击事件
	$('#'+tabId).off('click','.dep');//移除之前绑定的事件
	$('#'+tabId).on('click','.dep',function(){
		
		$('#'+tabId+' #depDatatable thead').html('');//清空
		$('#'+tabId+' #depDatatable tbody').html('');//清空
		
		$('#'+tabId+' .hosp').removeClass('depActive');
		$('#'+tabId+' .dep').removeClass('depActive');
		$(this).addClass('depActive');
		var pardepCode=$(this).attr('id');
		var depName=$(this).attr('title');
		paramTabPage[tabId]['pardepCode'] = pardepCode;
		paramTabPage[tabId]['depName'] = depName;
		$('#'+tabId+' .currDepOrDepgroup').html('<i class="fa fa-stethoscope"></i>'+depName);
		//加载表格
		loadQuotaData('dep',pardepCode,depName);
	});
	
	function loadQuotaData(monitorType,ccode,ddesc){
		
		
		if(paramTabPage[tabId].compareType=="year"){
			compareName = "同期";
		}else if(paramTabPage[tabId].compareType=="month"){
			compareName = "上期";
		}
		if(monitorType=="depGroup"){
			paramTabPage[tabId]['mtype']='depGroup';
		 }
		 if(monitorType=="dep"){
			 paramTabPage[tabId]['mtype']='dep';
		 }
		 if(monitorType=="hosp"){
			 ddesc='全院';
			 paramTabPage[tabId]['mtype']='hosp';
		 }
		 paramTabPage[tabId]['ccode']=ccode;
		 paramTabPage[tabId]['ddesc']=ddesc;
		//查询科室数据参数
		var depparm=drgUI.setAjaxData(tabId);
		depparm.monitorType=monitorType;
		//开始科室数据查询
		var depTableStructure = {"theadStr":"<tr>","tbodyStr":"<tr>"};
		for(var q in totalQuota[0].data){
			ajaxCntDepFeeStage.dep = ajaxCntDepFeeStage.dep +1;
			depparm["quotaCode"] = totalQuota[0].data[q].quotaCode;  
			var quotaName = totalQuota[0].data[q].quotaName;  
			buildTable(depTableStructure,depparm,quotaName,callback);
		}
		//科室数据查询
		function buildTable(depTableStructure,depparm,quotaName,callback){
			$.ajax({url: contextPath + "/DrgsIndexCal/indexCal.dhc","data":depparm, type:"post", success: function(data){
					ajaxCntDepFeeStage.dep = ajaxCntDepFeeStage.dep-1;
					depquotaData = data.data;
					callback(depTableStructure,quotaName, depquotaData);
				}
			});
		}
		//科室数据查询 回调 组装科室数据
		function callback(depTableStructure,quotaName, depquotaData){
			depTableStructure.theadStr+='<td>'+quotaName+'</td>';
			if((quotaName.indexOf("药占比") > -1)||(quotaName.indexOf("耗占比")>-1)){
				if(depquotaData!=null&&depquotaData.length>0){
					depTableStructure.tbodyStr+='<td>'+comparePercent(depquotaData[0].QuotaValue,depquotaData[0].QuotaValuecc)+'</td>';
				}
				else{
					depTableStructure.tbodyStr+='<td>'+comparePercent("","")+'</td>';
				}
			}
			else{
				if(depquotaData!=null&&depquotaData.length>0){
					depTableStructure.tbodyStr+='<td>'+compareValue(depquotaData[0].QuotaValue,depquotaData[0].QuotaValuecc)+'</td>';
				}
				else{
					depTableStructure.tbodyStr+='<td>'+compareValue(null,null)+'</td>';
				}
			}
		}
		//监测科室数据是否全部查询完
		var timer = setInterval(function(){
			if(ajaxCntDepFeeStage.dep==0){
				clearInterval(timer);
				depTableStructure.theadStr+='<td>操作</td></tr>';
				depTableStructure.tbodyStr+="<td><a class='toDepDrgFeeStage'><i class=\"fa fa-list-alt\"></i></a>&nbsp;&nbsp;" +
						      "<a class='feeStageOper'>医嘱</a></td></tr>";
				$('#'+tabId+' #depDatatable thead').html(depTableStructure.theadStr);
				$('#'+tabId+' #depDatatable tbody').html(depTableStructure.tbodyStr);
				$('#'+tabId+' #depDatatable tbody').off('click','.toDepDrgFeeStage');
				$('#'+tabId+' #depDatatable tbody').on('click','.toDepDrgFeeStage',function(){
					toDepDrgFeeStage(ccode, ddesc);
				})
				$('#'+tabId+' #depDatatable').off('click','.feeStageOper');
				$('#'+tabId+' #depDatatable').on('click','.feeStageOper',function(){
					orderDetail(ccode,ddesc,'','','depStageDep');
				});
			}
		}, 500);
		
		
		//主诊组
		var clinicParam=drgUI.setAjaxData(tabId);
		clinicParam.monitorType='clinic';
		clinicQuotaTableData=[];//表格数据
		clinicQuotaColumns=[{"data": null,"className": "details-control","width":25},{"data": "ddesc","title":"主诊组"}];//表格列
		var quotas = totalQuota[0].data;
		for(var o in quotas){
			(function(o){//解决for循环闭包问题
				ajaxCntDepFeeStage.clinic = ajaxCntDepFeeStage.clinic +1;
				var quotaCode = quotas[o].quotaCode;
				var quotaName = quotas[o].quotaName;
				clinicParam["quotaCode"] = quotaCode;
				
				if(quotaName.indexOf("%")>-1){//如果是比率类指标
					var column = { "data": quotaCode,"type":"number-beforeHtml","title":quotaName,
							"render":function(obj, type, full){
								return formatNumber(full[quotaCode])+gridcompareValuePercent(full[quotaCode],full[quotaCode+"cc"]);
							}};
					clinicQuotaColumns.push(column);
				}
				else{
					var column = { "data": quotaCode,"type":"number-beforeHtml","title":quotaName,
							"render":function(obj, type, full){
								return formatNumber(full[quotaCode])+gridcompareValue(full[quotaCode],full[quotaCode+"cc"]);
							}};
					clinicQuotaColumns.push(column);
				}
				buildClinicTable(clinicParam,quotaCode,quotaName,clinicCallBack);
			})(o);
		}
		//主诊组数据查询
		function buildClinicTable(clinicParam,quotaCode,quotaName,clinicCallBack){
			$.ajax({
				url : contextPath + "/DrgsIndexCal/indexCal.dhc",
				"data" : clinicParam,
				type : "post",
				success : function(data) {
					ajaxCntDepFeeStage.clinic = ajaxCntDepFeeStage.clinic-1;
					var clinicquotaData = data.data;
					clinicCallBack(quotaCode,quotaName,clinicquotaData);
				}
			});
		}
		//主诊组数据查询 回调 组装数据
		function clinicCallBack(quotaCode,quotaName,clinicquotaData){
			if(clinicQuotaTableData.length==0){//如果是第一次，除了指标值外，还需传入ccode ddesc
				for ( var cd in clinicquotaData)
				{
					var tmpCode = clinicquotaData[cd]["ccode"];
					var tmpdesc = clinicquotaData[cd]["ddesc"];
					var tmpQV = clinicquotaData[cd]["QuotaValue"];
					var tmpQVCC = clinicquotaData[cd]["QuotaValuecc"];
					
					var tmpStr = "{'ccode':'"+tmpCode+"','ddesc':'"+tmpdesc+"','"+quotaCode+"':'"+tmpQV+"','"+quotaCode+"cc':'"+tmpQVCC+"'}";
					var jsonobj=eval('('+tmpStr+')');
					clinicQuotaTableData[cd]=jsonobj;
				}
			}
			else{
				for ( var cd in clinicquotaData){
					var tmpclinicdata = clinicQuotaTableData[cd];
					tmpclinicdata[quotaCode]=clinicquotaData[cd]["QuotaValue"];
					tmpclinicdata[quotaCode+"cc"]=clinicquotaData[cd]["QuotaValuecc"];
					clinicQuotaTableData[cd]=tmpclinicdata;
				}
			}
		}
		//监测主诊组数据是否查询完
		var timerClinic = setInterval(function(){
			if(ajaxCntDepFeeStage.clinic==0){
				clearInterval(timerClinic);
				clinicQuotaColumns.push({ "data": "OPERATION","title":"操作","render": function(obj, type, full) {return "<a class='toClinicDrgFeeStage'><i class=\"fa fa-list-alt\"></i></a>&nbsp;&nbsp;<a class='feeStageOper'>医嘱</a>";}});
				$('#'+tabId+' #clinicTableDiv').html('<table id="clinicStageTable" class="table"></table>');
				var paramsDatatables={tableTitle:ddesc+'所辖主诊组的指标情况表',order:[2,'desc'],columnDefs:[{orderable:false,targets:[0,1,10]}]};
				var oTable =$('#'+tabId+' #clinicTableDiv #clinicStageTable').dataTableBasic(clinicQuotaColumns,clinicQuotaTableData,paramsDatatables);
				$('#'+tabId+' #clinicTableDiv #clinicStageTable').off('click','.feeStageOper');
				$('#'+tabId+' #clinicTableDiv #clinicStageTable').on('click','.feeStageOper',function(){
					var nTr = $(this).closest('tr');
					var row = oTable.row( nTr );
					var rowData=row.data();
					orderDetail(rowData.ccode, rowData.ddesc,'','', 'clinic');
				});
				$('#'+tabId+' #clinicTableDiv #clinicStageTable').off('click','.toClinicDrgFeeStage');
				$('#'+tabId+' #clinicTableDiv #clinicStageTable').on('click','.toClinicDrgFeeStage',function(){
					var nTr = $(this).closest('tr');
					var row = oTable.row( nTr );
					var rowData=row.data();
					toClinicDrgFeeStage(rowData.ccode, rowData.ddesc);
				});
				function format ( d ) {
					 return '<div class="col-xs-12" id="'+d.ccode+'">'
			           +'	<div class="row">'
			           +'		<div class="col-xs-6"><div class="stagePie" style="height:260px;"></div></div>'
			           +'		<div class="col-xs-6"><div class="stageLine" style="height:260px;"></div></div>'
			           +'	</div>'
			           +'   <div class="row">'
			           +'		<div class="col-xs-12">'
			           +'			<div class="stageTableDiv paddingd10"></div>'
			           +'     	</div>'
			           +'   </div>'
			           +'</div>';
				}
				//主诊组点击事件，查询主诊断下的分段科室的指标
				$('#'+tabId+' #clinicStageTable tbody').off("click", "td.details-control");
				$('#'+tabId+' #clinicStageTable tbody').on('click', 'td.details-control', function () {
					var nTr = $(this).closest('tr');
					var row = oTable.row( nTr );
					var aData=row.data();
					if (row.child.isShown()){ //判断是否已打开
						row.child.hide();
						nTr.removeClass("shown");
					} else {
						nTr.addClass("shown");
						row.child(format(aData)).show();
						echartLoadingInit($('#'+tabId+' #'+aData.ccode+' .stageLine'));//loading效果
						echartLoadingInit($('#'+tabId+' #'+aData.ccode+' .stagePie'));//loading效果
						
			            //主诊组分段科室表格
				        var tid='id'+new Date().getTime();
						$('#'+tabId+' #'+aData.ccode+' .stageTableDiv').html('<table id="'+tid+'" class="table"></table>')
						var param=drgUI.setAjaxData(tabId);
						param.monitorType=monitorType;
						param.type1=paramTabPage[tabId]['orderSpecialDep']==''?"":paramTabPage[tabId].orderSpecialDep+",'"+paramTabPage[tabId].depName+"'";
						param.pargroupName=aData.ccode;
						$.post(contextPath+"/feeStage/getFeeStageData.dhc", param).done(function(data){
							data = data.data;
							var columns = [
											{ "data": "STAGEDEP","title":"部门"},
						                    { "data": "TOTALCASECNT","type":"number-html","title":"患者数",
						                      "render": function(obj, type, full) {
						                    	  return compareValue(full.TOTALCASECNT, full.TOTALCASECNTcc);
						                      }
						                    },
						                    { "data": "STAGEFEE","type":"number-html","title":"分段费用(万元)",
						                      "render": function(obj, type, full) {
						                    	  return compareValue(full.STAGEFEE, full.STAGEFEEcc);
						                      }
						                    },
						                    { "data": "StageRatio","type":"number-html","title":"分段费用占比(%)",
						                      "render": function(obj, type, full) {
						                    	  return compareValue(full.STAGERATIO, full.STAGERATIOcc);
						                      }
						                    },
						                    { "data": "AvgFee","type":"number-html","title":"次均费用(元)",
						                      "render": function(obj, type, full) {
						                    	  return compareValue(full.AVGFEE, full.AVGFEEcc);
						                      }
						                    },
						                    { "data": "DrugFee","type":"number-html","title":"药品费用(万元)",
						                      "render": function(obj, type, full) {
						                    	  return compareValue(full.DRUGFEE, full.DRUGFEEcc);
						                      }
						                    },
						                    { "data": "DrugRatio","type":"number-html","title":"药占比(%)",
						                      "render": function(obj, type, full) {
						                    	  return comparePercent(full.DRUGRATIO, full.DRUGRATIOcc);
						                      }
						                    },
						                    { "data": "MaterialFee","type":"number-html","title":"耗材费(万元)",
						                      "render": function(obj, type, full) {
						                    	  return compareValue(full.MATERIALFEE, full.MATERIALFEEcc);
						                      }
						                    },
						                    { "data": "MaterialRatio","type":"number-html","title":"耗占比(%)",
						                      "render": function(obj, type, full) {
						                    	  return comparePercent(full.MATERIALRATIO, full.MATERIALRATIOcc);
						                      }
						                    },
						                    { "data": "OrderQuantity","type":"number-html","title":"医嘱项数",
							                      "render": function(obj, type, full) {
							                    	  return compareValue(full.ORDERQUANTITY, full.ORDERQUANTITYcc);
							                      }
						                    },
						                    { "data": "OPERATION","title":"操作","className":"feeStageOperClinic",
						                      "render": function(obj, type, full) {
						                    	  return "<a>医嘱</a>";
						                      }
						                    }
						                 ];
							var paramsDatatable={searching:false,lengthChange:false,info:false,paging:false,tableTitle:aData.ddesc+'费用分段指标情况表',order:[2,'desc'],columnsDefs:[{orderabel:false,targets:[0,1,10]}]};
							if(data.length>12){
								paramsDatable.scrollY='350px';
							}
							var clinicStageTable=$('#'+tabId+' #'+tid).dataTableBasic(columns,data,paramsDatatable);
							$('#'+tabId+' #'+tid).off('click');
							$('#'+tabId+' #'+tid).on('click','.feeStageOperClinic a',function(){
								var nTr = $(this).closest('tr');
								var row = clinicStageTable.row( nTr );
								var rowData=row.data();
								//特殊部门未配置且，当前分段科室为其他时，需特殊处理,拼接现有表格中除其他以外的分段科室
								if(rowData.DEPCATE=='OTHER'&&paramTabPage[tabId].orderSpecialDep==''){
									var noStageDepDescArray=[];
									var tableData=clinicStageTable.data().toArray();
									for(var o in tableData){
										if(tableData[o].DEPCATE!='OTHER'){
											noStageDepDescArray.push("'"+tableData[o].STAGEDEP+"'");
										}
									}
									var noStageDepDesc=noStageDepDescArray.join(",");
									orderDetail(aData.ccode,aData.ddesc,rowData.DEPCATE, rowData.STAGEDEP, 'clinicStageDep',noStageDepDesc);
								}
								else{
									orderDetail(aData.ccode,aData.ddesc,rowData.DEPCATE, rowData.STAGEDEP, 'clinicStageDep');
								}
								
							});
							//组装饼图数据
							var stageFeeData = [];
							var avgFeeData = [];
							var drugRatioData = [];
							var avgFeeDatacc = [];
							var drugRatioDatacc = [];
							var stageDepData = [];
							for(var n in data){
								stageFeeData.push({name:data[n].STAGEDEP,value:data[n].STAGEFEE});
								avgFeeData.push(data[n].AVGFEE);
								drugRatioData.push(data[n].DRUGRATIO);
								avgFeeDatacc.push(data[n].AVGFEEcc);
								drugRatioDatacc.push(data[n].DRUGRATIOcc);
								stageDepData.push(data[n].STAGEDEP);
							}
							var charParams={pagemode:true,radius:[0, '50%']};
							pieCharts($('#'+tabId+' #'+aData.ccode+' .stagePie'), stageFeeData, '特殊部门费用分布图', '',charParams);
							var legend=['次均费用(本期)','次均费用('+compareName+')','药占比(本期)','药占比('+compareName+')'];
							var xAxis = [{type : 'category',data : stageDepData}];
							var yAxis = [{type : 'value',name : '次均费用',splitNumber:5,axisLabel : {formatter: '{value} 元'}},
								        {type : 'value',name : '药占比',splitLine:{show:false},axisLabel : {formatter: '{value} %'}}];
							var series = [
									        {name:'次均费用(本期)',type:'bar',data:avgFeeData},
									        {name:'次均费用('+compareName+')',type:'bar',data:avgFeeDatacc},
									        {name:'药占比(本期)',type:'line',yAxisIndex: 1,data:drugRatioData},
									        {name:'药占比('+compareName+')',type:'line',yAxisIndex: 1,data:drugRatioDatacc}
									    ];
							var chartParams={};
							barLineMixChartBasic($('#'+tabId+' #'+aData.ccode+' .stageLine'), legend,series,xAxis,yAxis,'特殊部门次均费用和药占比','',chartParams);
						});
			        }
			    } );
				/*-----------------------------------------主诊组点击事件完毕-------------------------------------------------*/	
			}
		}, 500);
		/*-----------------------------------------主诊组数据加载完毕-------------------------------------------------*/	
		
		//科室---分段科室表格
		$('#'+tabId+' #depStageTableDiv').html('<table id="depStageTable" class="table nowrap"></table>');
		echartLoadingInit($('#'+tabId+' #depStagePie'));//loading效果
		echartLoadingInit($('#'+tabId+' #depStageLine'));//loading效果
		var param=drgUI.setAjaxData(tabId);
		param.monitorType=monitorType;
		param.type1=paramTabPage[tabId]['orderSpecialDep']==''?"":paramTabPage[tabId].orderSpecialDep+",'"+paramTabPage[tabId].depName+"'";
		$.post(contextPath+"/feeStage/getFeeStageData.dhc", param).done(function(data){
			data = data.data;
			var columns = [
							{ "data": "STAGEDEP","title":"部门"},
		                    { "data": "TOTALCASECNT","type":"number-html","title":"患者数",
		                      "render": function(obj, type, full) {
		                    	  return compareValue(full.TOTALCASECNT, full.TOTALCASECNTcc);
		                      }
		                    },
		                    { "data": "STAGEFEE","type":"number-html","title":"分段费用(万元)",
		                      "render": function(obj, type, full) {
		                    	  return compareValue(full.STAGEFEE, full.STAGEFEEcc);
		                      }
		                    },
		                    { "data": "StageRatio","type":"number-html","title":"分段费用占比(%)",
		                      "render": function(obj, type, full) {
		                    	  return compareValue(full.STAGERATIO, full.STAGERATIOcc);
		                      }
		                    },
		                    { "data": "AvgFee","type":"number-html","title":"次均费用(元)",
		                      "render": function(obj, type, full) {
		                    	  return compareValue(full.AVGFEE, full.AVGFEEcc);
		                      }
		                    },
		                    { "data": "DrugFee","type":"number-html","title":"药品费用(万元)",
		                      "render": function(obj, type, full) {
		                    	  return compareValue(full.DRUGFEE, full.DRUGFEEcc);
		                      }
		                    },
		                    { "data": "DrugRatio","type":"number-html","title":"药占比(%)",
		                      "render": function(obj, type, full) {
		                    	  return comparePercent(full.DRUGRATIO, full.DRUGRATIOcc);
		                      }
		                    },
		                    { "data": "MaterialFee","type":"number-html","title":"耗材费(万元)",
		                      "render": function(obj, type, full) {
		                    	  return compareValue(full.MATERIALFEE, full.MATERIALFEEcc);
		                      }
		                    },
		                    { "data": "MaterialRatio","type":"number-html","title":"耗占比(%)",
		                      "render": function(obj, type, full) {
		                    	  return comparePercent(full.MATERIALRATIO, full.MATERIALRATIOcc);
		                      }
		                    },
		                    { "data": "OrderQuantity","type":"number-html","title":"医嘱项数",
			                      "render": function(obj, type, full) {
			                    	  return compareValue(full.ORDERQUANTITY, full.ORDERQUANTITYcc);
			                      }
		                    },
		                    { "data": "OPERATION","title":"操作","className":"feeStageOper",
		                      "render": function(obj, type, full) {
		                    	  return "<a>医嘱</a>";
		                      }
		                    }
		                 ];
			var paramDatatables={searching:false,paging:false,scorllX:true,lengthChange:false,info:false,tableTitle:ddesc+'科室费用分段指标情况表',order:[2,'desc'],columnDefs:[{"orderable": false,"targets": [ 0,10 ] }]};
			if(data.length>12){
				paramDatatables.scrollY='350px';
			}
			var depStageTable=$('#'+tabId+' #depStageTable').dataTableBasic(columns,data,paramDatatables);
			$('#'+tabId+' #depStageTable').off('click');
			$('#'+tabId+' #depStageTable').on('click','.feeStageOper a',function(){
				var nTr = $(this).closest('tr');
				var row = depStageTable.row( nTr );
				var rowData=row.data();
				//特殊部门未配置且，当前分段科室为其他时，需特殊处理,拼接现有表格中除其他以外的分段科室
				if(rowData.DEPCATE=='OTHER'&&paramTabPage[tabId].orderSpecialDep==''){
					var noStageDepDescArray=[];
					var tableData=depStageTable.data().toArray();
					for(var o in tableData){
						if(tableData[o].DEPCATE!='OTHER'){
							noStageDepDescArray.push("'"+tableData[o].STAGEDEP+"'");
						}
					}
					var noStageDepDesc=noStageDepDescArray.join(",");
					orderDetail(ccode,ddesc,rowData.DEPCATE, rowData.STAGEDEP,'depStageDep',noStageDepDesc);
				}
				else{
					orderDetail(ccode,ddesc,rowData.DEPCATE, rowData.STAGEDEP,'depStageDep');
				}
			});
			//组装饼图数据
			var stageFeeData = [];
			var avgFeeData = [];
			var drugRatioData = [];
			var avgFeeDatacc = [];
			var drugRatioDatacc = [];
			var stageDepData = [];
			for(var n in data){
				stageFeeData.push({name:data[n].STAGEDEP,value:data[n].STAGEFEE});
				avgFeeData.push(data[n].AVGFEE);
				drugRatioData.push(data[n].DRUGRATIO);
				avgFeeDatacc.push(data[n].AVGFEEcc);
				drugRatioDatacc.push(data[n].DRUGRATIOcc?data[n].DRUGRATIOcc:"");
				stageDepData.push(data[n].STAGEDEP);
			}
			var charParams={pagemode:true,radius:[0, '50%']};
			pieCharts($('#'+tabId+' #depStagePie'), stageFeeData, '特殊部门费用分布图', '',charParams);
			
			var legend=['次均费用(本期)','次均费用('+compareName+')','药占比(本期)','药占比('+compareName+')'];
			var xAxis = [{type : 'category',data : stageDepData}];
			var yAxis = [{type : 'value',name : '次均费用',splitNumber:5,axisLabel : {formatter: '{value} 元'}},
				        {type : 'value',name : '药占比',splitLine:{show:false},axisLabel : {formatter: '{value} %'}}];
			var series = [
					        {name:'次均费用(本期)',type:'bar',data:avgFeeData},
					        {name:'次均费用('+compareName+')',type:'bar',data:avgFeeDatacc},
					        {name:'药占比(本期)',type:'line',yAxisIndex: 1,data:drugRatioData},
					        {name:'药占比('+compareName+')',type:'line',yAxisIndex: 1,data:drugRatioDatacc}
					    ];
			var chartParams={};
			barLineMixChartBasic($('#'+tabId+' #depStageLine'), legend,series,xAxis,yAxis,'特殊部门次均费用和药占比','',chartParams);
		});
		/*-----------------------------------------科室层分段科室数据加载完毕-------------------------------------------------*/
	};
	//科室病种费用分段
	function toDepDrgFeeStage(ccode,ddesc){
		var param=jQuery.extend(true, {},paramTabPage[tabId]);
		param['ccode'] = ccode;
		param['ddesc'] = ddesc;
		param['mtype'] = 'dep';
		openTab('/pages/feeStage/depDrgFeeStage.jsp', ddesc+"|"+'病种费用分段', 'depDrgFeeStage', 'depDrgFeeStage', param);
	}

	//主诊组病种费用分段
	function toClinicDrgFeeStage(ccode,ddesc){
		var param=jQuery.extend(true, {},paramTabPage[tabId]);
		param['groupName'] = ccode;
		param['ccode'] = ccode;
		param['ddesc'] = ddesc;
		param['mtype'] = 'clinic';
		openTab('/pages/feeStage/depDrgFeeStage.jsp', ddesc+"|"+'病种费用分段', 'clinicDrgFeeStage', 'clinicDrgFeeStage', param);
	}
	//医嘱弹出框
	function orderDetail(typeCode,typeDesc,stageDepCode, stageDepDesc, type,noStageDepDesc){
		var param=drgUI.setAjaxData(tabId);
		 if(type=="clinic"){
			 param.pargroupName=typeCode;
		 }
		 if(type=='dep'){
			 param.pardepCode=typeCode;
		 }
		 if(type=="clinicStageDep"){
			 param.pargroupName=typeCode;
			 if(stageDepCode=='OTHER'){//如果是其他，则传入特殊部门
				 if(paramTabPage[tabId].orderSpecialDep==''){//没有配置特殊部门时
					 param.type1=noStageDepDesc;
				 }
				 else{
					 param.type1=paramTabPage[tabId].orderSpecialDep+",'"+paramTabPage[tabId].depName+"'";
				 }
				
			 }
			 else{
				 param.type1=stageDepDesc;
			 }
		 }
		 if(type=="depStageDep"){
			 param.pardepCode=typeCode;
			 if(stageDepCode=='OTHER'){//如果是其他，则传入特殊部门
				 if(paramTabPage[tabId].orderSpecialDep==''){//没有配置特殊部门时
					 param.type1=noStageDepDesc;
				 }
				 else{
					 param.type1=paramTabPage[tabId].orderSpecialDep+",'"+paramTabPage[tabId].depName+"'";
				 }
			 }
			 else{
				 param.type1=stageDepDesc;
			 }
		 }
		 $.post(contextPath + '/pages/common/top-dialog.jsp').done(function(data){
				var dialog  = $(data);
				dialog.find(".modal-title").html(typeDesc+"-"+(stageDepDesc==""?"":(stageDepDesc+"-"))+"医嘱项明细");
				dialog.find(".modal-body").css("min-height", "440px");
				dialog.find(".modal-body").css("width", "60%");
				dialog.modal({
					keyboard: false
				}).on({
					'hidden.bs.modal' : function(){
						$(this).remove();
					},
					'shown.bs.modal' : function(){
						$.post(contextPath + "/feeStage/getOrderData.dhc",param).done(function(data){
							var data = data.data;
							//分段科室表格
							dialog.find(".echartscq").html('<table class="table display nowrap"></table>');
							var columns = [
								{ "data": "ORDERID","title":"医嘱序号"},
			                    { "data": "ORDERDESC","title":"医嘱项名称","render": function(obj, type, full) {
				   	            	 return linefeedText(obj);
				   	            }
								},
			                    { "data": "ORDERTOTALFEE","title":"医嘱总额(元)",
			                    	"render":function(obj, type, full){
			                    		return formatNumber(full.ORDERTOTALFEE);
			                    	}
			                    },
			                    { "data": "ORDERFEERATIO","title":"金额占比(%)"},
			                    { "data": "AvgOrderFee","title":"次均医嘱金额(元)",
			                    	"render":function(obj, type, full){
			                    		return formatNumber(full.AVGORDERFEE);
			                    	}
			                    },
			                    { "data": "OrderUnitPrice","title":"单价",
			                    	"render":function(obj, type, full){
			                    		return formatNumber(full.ORDERUNITPRICE);
			                    	}
			                    },
			                    { "data": "CaseCnt","title":"使用人次",
			                    	"render":function(obj, type, full){
			                    		return formatNumber(full.CASECNT);
			                    	}
			                    },
			                    { "data": "OrderQuantity","title":"医嘱数量",
			                    	"render":function(obj, type, full){
			                    		return formatNumber(full.ORDERQUANTITY);
			                    	}
			                    }
			                 ];
							
							var paramsDatatable={tableTitle:'医嘱明细表',scrollX:true, scrollY:'300px'};
							dialog.find(".echartscq").find('table').dataTableBasic(columns, data,paramsDatatable);
						   });
					}
				});
		 });
	}
});